/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.modudf;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class CMD {

    public static void openURL(String url) {
        try {
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd.exe /c start " + url);
        } catch (IOException ex) {
            Logger.getLogger(CMD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static int process(String bat) {
        try {
            final Process process = Runtime.getRuntime().exec("cmd.exe");
            final OutputStream os = process.getOutputStream();
            new Thread(new ProcessCMD(process.getErrorStream(), System.err)).start();
            new Thread(new ProcessCMD(process.getInputStream(), System.out)).start();
            final InputStream batStream = CMD.class.getResourceAsStream(bat);
            new ProcessCMD(batStream, os).run();
            int code = process.waitFor();
            if (code != 0) {
                Logger.getLogger(CMD.class.getName())
                        .log(Level.SEVERE, "process errorCode:" + code);
            }
            return code;
        } catch (IOException ex) {
            Logger.getLogger(CMD.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ie) {
            Logger.getLogger(CMD.class.getName()).log(Level.SEVERE, null, ie);
        }
        return -1;
    }
    
    public static void checkCode(int code, String message) {
        switch (code) {
            case 0: // successful
                break;
            case 1:
                //MyDialog.show(message + ":" + code);
                break;
            case 1056:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":服务正在运行中..." + code);
                break;
            case 1058:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":无法启动服务," +
                        "原因可能是已被禁用或与其相关联的设备没有启动." +
                        "稍后重试,如果仍然出错,请重新启动计算机!" + code);
                break;
            case 1060:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":指定的服务未安装!" + code);
                break;
            case 1061:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":服务无法在此时接受控制信息." + code);
                break;
            case 1072:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":指定的服务已标记为删除,如果错误仍存在," +
                        "请重启计算机" + code);
                break;
            default:
                JOptionPane.showMessageDialog(new JPanel(),
                        message + ":未知错误!" + code);
                break;
        }
    }
}
